#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 图书结构体
typedef struct {
	char id[20], name[50], author[30];
	int stock;
} Book;

// 借阅记录结构体
typedef struct {
	char id[20], borrow[20], ret[20];
	int returned;
} Record;

char bookFile[50] = "D:\\sdesf\\Book.txt", recFile[50] = "D:\\sdesf\\Record.txt";

// 检查图书是否存在
int bookExists(char *id) {
	FILE *fp = fopen(bookFile, "r");
	if (!fp) return 0;
	
	Book b;
	while (fscanf(fp, "%s %s %s %d", b.id, b.name, b.author, &b.stock) == 4) {
		if (!strcmp(b.id, id)) {
			fclose(fp);
			return 1;
		}
	}
	fclose(fp);
	return 0;
}

// 获取图书库存
int getStock(char *id) {
	FILE *fp = fopen(bookFile, "r");
	Book b;
	while (fscanf(fp, "%s %s %s %d", b.id, b.name, b.author, &b.stock) == 4) {
		if (!strcmp(b.id, id)) {
			fclose(fp);
			return b.stock;
		}
	}
	fclose(fp);
	return -1;
}

// 更新库存
void updateStock(char *id, int change) {
	FILE *fp = fopen(bookFile, "r"), *tmp = fopen("tmp.txt", "w");
	Book b;
	
	while (fscanf(fp, "%s %s %s %d", b.id, b.name, b.author, &b.stock) == 4) {
		if (!strcmp(b.id, id)) b.stock += change;
		fprintf(tmp, "%s %s %s %d\n", b.id, b.name, b.author, b.stock);
	}
	
	fclose(fp); fclose(tmp);
	remove(bookFile); rename("tmp.txt", bookFile);
}

// 添加图书
void addBook() {
    Book b;
	printf("书号: "); scanf("%s", b.id);
	if (bookExists(b.id)) {
		printf("书号已存在!\n");
		return;
	}
	
	printf("书名: "); scanf("%s", b.name);
	printf("作者: "); scanf("%s", b.author);
	printf("库存(可0): "); scanf("%d", &b.stock);
	while (getchar() != '\n'); 
	if (b.stock < 0) {
		printf("库存不能为负!\n");
		return;
	}
	
	FILE *fp = fopen(bookFile, "a");
	fprintf(fp, "%s %s %s %d\n", b.id, b.name, b.author, b.stock);
	fclose(fp);
	printf("添加成功!\n");
}

// 借阅图书
void borrowBook() {
	char id[20];
	printf("借阅书号: "); scanf("%s", id);
	
	if (!bookExists(id)) {
		printf("图书不存在!\n");
		return;
	}
	
	if (getStock(id) <= 0) {
		printf("库存不足!\n");
		return;
	}
	
	// 生成借阅记录
	Record r;
	strcpy(r.id, id);
	r.returned = 0;
	time_t t = time(NULL);
	strftime(r.borrow, 20, "1", localtime(&t));
	strcpy(r.ret, "未归还");
	
	FILE *fp = fopen(recFile, "a");
	fprintf(fp, "%s %s %s %d\n", r.id, r.borrow, r.ret, r.returned);
	fclose(fp);
	updateStock(id, -1);
	printf("借阅成功! 时间: %s\n", r.borrow);
}

// 归还图书
void returnBook() {
	char id[20];
	printf("归还书号: "); scanf("%s", id);
	
	FILE *fp = fopen(recFile, "r"), *tmp = fopen("tmp_rec.txt", "w");
	Record r;
	int found = 0;
	time_t t = time(NULL);
	char now[20];
	strftime(now, 20, "%Y-%m-%d", localtime(&t));
	
	while (fscanf(fp, "%s %s %s %d", r.id, r.borrow, r.ret, &r.returned) == 4) {
		if (!found && !strcmp(r.id, id) && !r.returned) {
			r.returned = 1;
			strcpy(r.ret, now);
			found = 1;
		}
		fprintf(tmp, "%s %s %s %d\n", r.id, r.borrow, r.ret, r.returned);
	}
	
	fclose(fp); fclose(tmp);
	remove(recFile); rename("tmp_rec.txt", recFile);
	
	if (found) {
		updateStock(id, 1);
		printf("归还成功! 时间: %s\n", now);
	} else {
		printf("无未归还记录!\n");
	}
}

// 显示图书
void showBooks() {
	FILE *fp = fopen(bookFile, "r");
	if (!fp) {
		printf("无图书数据!\n");
		return;
	}
	
	printf("\n===== 图书列表 =====\n");
	printf("书号       书名         作者       库存\n");
	Book b;
	while (fscanf(fp, "%s %s %s %d", b.id, b.name, b.author, &b.stock) == 4) {
		printf("%-10s %-12s %-10s %d\n", b.id, b.name, b.author, b.stock);
	}
	fclose(fp);
}

// 显示记录
void showRecords() {
	FILE *fp = fopen(recFile, "r");
	if (!fp) {
		printf("无借阅记录!\n");
		return;
	}
	
	printf("\n===== 借阅记录 =====\n");
	printf("书号       借阅时间     归还时间     状态\n");
	Record r;
	while (fscanf(fp, "%s %s %s %d", r.id, r.borrow, r.ret, &r.returned) == 4) {
		printf("%-10s %-12s %-12s %s\n", r.id, r.borrow, r.ret, 
			r.returned ? "已归还" : "未归还");
	}
	fclose(fp);
}

int main(){

	int choice;
	printf("图书存储路径: %s\n", bookFile);
	printf("记录存储路径: %s\n", recFile);
	
	for(int i=0;;i++){
		printf("\n===== 图书管理系统 =====\n");
		printf("1.添加图书  2.借阅  3.归还  4.显示图书  5.显示记录  6.退出\n");
		printf("选择: "); scanf("%d", &choice);
	while (getchar() != '\n'); 
		switch (choice) {
			case 1: addBook(); break;
			case 2: borrowBook(); break;
			case 3: returnBook(); break;
			case 4: showBooks(); break;
			case 5: showRecords(); break;
			case 6: printf("再见!\n"); return 0;
			default: printf("无效选择!\n");
		}
	
   } 
}

